<?php
function mangaf2_flush_manga($id){
			$memcache = new Memcache;
			$memcache->connect('localhost', 11211);
			$memcache->delete('anime'.$id);
}
function mangaf2_get_manga($id){
	$id=intval($id);
	
	$cache=new cache;
	$dt=$cache->load("anime".$id,"anime",1);
	if(!$dt){
		$db=new db;
		$db->connect();
		$db->query("select * from anime where id=".$id);
		if($db->num_rows()>0){
			$dt=$db->fetchOne();
		
			
			
			 		
					 //Fetch tinh trang
					if($dt['tinhtrang']==1){	$dt['tinhtrang-text']="<a href='".HOME."truyentranh/status/full.html'>Full</a>";
					}else	if($dt['tinhtrang']==2){$dt['tinhtrang-text']= "<a href='".HOME."truyentranh/status/tam-ngung.html'>Tạm Ngưng</a>";
					}else{$dt['tinhtrang-text']= "<a href='".HOME."truyentranh/status/ongoing.html'>Ongoing</a>";	}
					
					$html = str_get_html($dt['noidung']);
					$dt['noidung']=$html->plaintext;
					
					// Update table the loai cai coi
					 $sql="update anime_theloai_fk set views=".$dt['views'].",duyet=".$dt['duyet'].",comments=".$dt['comments'].",upload_time='".$dt['upload_time']."',tinhtrang=".$dt['tinhtrang'].",update_time='".$dt['update_time']."',tongsoep=".$dt['tongsoep'].",view_week=".$dt['view_week'].",view_month=".$dt['view_month'].",thanks=".$dt['thanks']." where anime_id=".$id ;
		
				$db->nonquery($sql);
				
				//Update ep_moinhat,tongsoep,link chapter moi nhat
				//ep_moinhaturl
				$db->query("select id,ten,seourl,ngaythem from chapterk2 where manga=".$dt['id']." order by ngaythem desc, thutu desc limit 0,1");
				$r1=$db->fetchOne();
				
				$db->query("select count(*) as num from chapterk2 where manga=".$id);
				$r2=$db->fetchOne();
				$dt['ep_num']=$r2['num'];
				$dt['ep_ngaythem']=$r1['ngaythem'];
				$dt['ep_moinhat']=$r1['ten'];
				$dt['ep_moinhaturl']=mangaf1_url_manga_episode($r1['id'],$r1['seourl'],$dt['alias']);
				$db->nonquery("update anime set tongsoep=".$dt['ep_num'].",ep_moinhat='".$dt['ep_moinhat']."',ep_moinhaturl='".$dt['ep_moinhaturl']."',update_time='".$dt['ep_ngaythem']."' where id=".$id);
				//echo $dt['ep_moinhat'];
					 
				
				
									   
									   
					 //GET ANIME LIEN QUAN-----
									
					if($dt['tags']!=''){
							   $db->query("select name,id,alias,hinhanh,ep_moinhat,ep_moinhaturl,theloai,nguon,gioithieu from anime where tags like'%".$dt['tags']."%' and id!= ".$id);
							   if($db->num_rows()>0){
								$dt['related']=$db->fetchAll();
											   }
					}
									   
					//------ end get anime lien quan
					//Close ket noi db	
					
					//----------------- CREATE MANGA TAG
					if($dt['gtag']==0){
						$tagstring=mangaf1_tag1_generate($dt['name']);
						mangaf1_manga_tag1($dt['id'],$tagstring);
					$db=new db;
					$db->connect();
					$db->nonquery("update anime set gtag=1 where id=".$dt['id']);
					}
					//Generate tags phat da
								   
					 $db->close();
					 //Create tag--
					 $dt['data_tags']=mangaf1_tag1_link($dt['name']);
					 //------------
					 
					 //Fetch the loai de lay link
					$dt['theloai2']=mangaf2_get_manga_theloai($dt['theloai']);
						//Tag-
					$dt['tag']=mangaf2_manga_get_tag($dt['tags']);
					//GET EP CO THU TU LON NHAT RA
					
					//GET LIST CHUONG --------------------
					$db=new db;
					$db->connect();
					$db->query("select id,ten,seourl,index_show,ngaythem,uploader,solanxem from chapterk2 where manga=".$id." and xoa!=1 order by thutu desc");
					if($db->num_rows()>0){
						$dt['chapters']=$db->fetchAll();
					}
					$db->close();
					
				//tag3------
				//Tag3 update----------------
					if($dt['tag3']==''||$dt['tag3']==0){
							//Select 3 ep co thutu lon nhat
							$db=new db;
							$db->connect();
							$db->query("select id,ten,thutu from chapterk2 where manga=".intval($id)." and xoa !=1 order by thutu desc limit 0,5");
							
							$tt='';
									if($db->num_rows()>0){
										$dk=$db->fetchAll();
										foreach($dk as $row){
											if(strpos($row['ten'],$row['thutu'])!==false){
												$tt=$row['thutu'];
												break;
											}
										}
									}
							
							if($tt!=''){
								$tt=floor($tt);
								$db->nonquery("update anime set tag3=$tt where id=".intval($id));
								$db->close();
								mangaf1_tag3_generate($dt['name'],$tt,$dt['id']);
								//$cache=new cache;
								//$cache->flush("anime".$id,"anime");
							}else{
								$db->close();
							}
							
					}
	
					//get tag3
					$db=new db;
					$db->connect();
					$db->query("select name from anime_tag3 where anime_id=".intval($id));
					if($db->num_rows()>0){
						$tag=$db->fetchAll();
						foreach($tag as $rc){
						//Noi chuoi vao thoi
						$tags.=$rc['name'].",";
						
						}
					
						//echo $tags;
						$dt['tag3']=mangaf1_tag3_link($tags);
					}
					$db->close();
			
					
			
			$cache->save("anime".$id,$dt,3600,"anime",1);
		}else{
			
			return false;
			} // End neu khong co anime nay
	}

	
	return $dt;
}



function mangaf2_get_ep($id){
	
	$id=intval($id);
	$cache=new cache;
	$sub=$id%100;
	//Dau tien la check ngay them manga nay bao lau roi
	$db=new Model;
	
	$db->query("select * from chapterk_time where chap_id =".$id);
	if($db->num_rows()==0){
			$db->query("select ngaythem from chapterk2 where id=".$id);
			$r1=$db->fetchOne();
			$datam=array("chap_id"=>$id,"time"=>$r1['ngaythem']);
			$db->insert("chapterk_time",$datam);
			$ngaythem=$r1['ngaythem'];
	}else{
		  $r1=$db->fetchOne();
		  $ngaythem=$r1['time'];	
	}
	//So sanh ngay
	if($ngaythem > date('Y-m-d',strtotime('-1 day'))){
	
	//Chapter nay moi add trong vong 2 ngay, nen cache 
	$try_cache=true;
	}else{$ry_cache=false; }
	//
	if($try_cache){

	$data=$cache->load('manga_get_chapter_detail'.$id,'chapter/'.$sub);
	}else{
	
	$data=false;
	}
	
	if(!$data){
	
			$mg=new Model;
			//---------- GET MANGA INFORMATION
			$mg->where="id= $id";
			$mg->query("select * from chapterk2 where id=".$id);
			$row=$mg->fetchOne();	
			$row['url']=mangaf1_url_manga_episode($row['id'],$row['seourl']);
			
			//-------- CHECK AND FIX IMAGE LINK -----------------
	
					
						if($row['op']==1){
						
							$db->query("select * from chapterk_content where chap_id=".$id);
							//echo mysql_error();
							if($db->num_rows()>0){
								$r2=$db->fetchOne();
								
								//Chapterk3_content dung de chua link remote upload len picasa cua image co tren server Manga24h-----
								$db->query("select * from chapterk3_content where chap_id=".$id);
								$num=$db->num_rows();
							//	if($r2['server']=='manga24h.com'&&$num>0&$r2['check_link']==1&&$r2['check_error']==0){
								if($r2['server']=='manga24h.com'&&$num>0){
								
									//GET TU TABLE CHAPTERK3_CONTENT
								
									$r2=$db->fetchOne();
									$row['chapter_link']=unserialize($r2['picasa']);
									$row['chapter_page']=$r2['chap_page'];
								}else{
								
											$row['chapter_link']=unserialize($r2['chap_content']);
											$link=$row['chapter_link'][0];
											
											
											if(strpos($link,'vnsharing')!==false){
											$r5['server']='manga24h.com';
											$r5['error']=0;
											}else
											if(strpos($link,'manga24h.com')!==false){
											$r5['server']='manga24h.com';
											$r5['error']=0;
											}else
											if(strpos($link,'upimgs.org')!==false){
											$r5['server']='upimgs';
											$r5['error']=1;
											}else
											if(strpos($link,'zdn.vn')!==false){
											$r5['server']='zing';
											$r5['error']=0;
											}
											else
											if(strpos($link,'upanh.com')!==false){
											$r5['server']='upanh';
											$r5['error']=1;
											}
											else
											if(strpos($link,'blogspot.com')!==false){
											$r5['server']='blogspot';
											$r5['error']=0;
											}else{
											$r5['server']='khac';
											$r5['error']=0;
											}
											
											
											
											$row['chapter_page']=$r2['chap_page'];
											
											
											$db->where("chap_id=".$id);
											$db->update("chapterk_content",$r5);
								}
								
							}
							
						}else{
							//Start get link page 
							//if($row['upload']==1 && $row['zing']==0 && $row['blog']==0 && $row['upanh']==0)
							if(0)
							{	
								
								
								$db->query("select * from mangadata where id=".$row['id']);
								$row_data=$db->fetch();
								$row_image=unserialize($row_data[data]);
								$h=$row_image['pages'];
								natsort($row_image['images']);
								$image=$row_image['images'];
								$image2=array();
								foreach($image as $value)
								{
								$image2[]="http://data2.manga24h.com/".$value;
								}
								
								$row['chapter_link']=$image2;
								$row['chapter_page']=	$h;	
								
								//Else of getlink	
							}else{
								
								
								//=========================Lay link image tu ngoai vao================
								$row['chapter_link']=array();
								$source=$row['source'];
								
								
									
								
								preg_match_all('/(img|src)\=(\"|\')[^\"\'\>]+/i', $source, $media);
								$datasource=preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
								$images = array();
								$h=0;
								foreach($datasource as $url)
								{
											//preplace zing link......
														$new=preg_replace('/img.photo.zing.vn/',"d.f2.photo.zdn.vn",$url);
											  		   $bl=preg_replace('/lh3.ggpht.com/',"1.bp.blogspot.com",$new);
														$bl1=preg_replace('/lh4.ggpht.com/',"2.bp.blogspot.com",$bl);
														$bl2=preg_replace('/lh5.ggpht.com/',"3.bp.blogspot.com",$bl1);
														$bl3=preg_replace('/lh6.ggpht.com/',"4.bp.blogspot.com",$bl2);
														$bl4=preg_replace('/https:\/\/lh3.googleusercontent.com/',"http://3.bp.blogspot.com",$bl3);
														$bl5=preg_replace('/https:\/\/lh4.googleusercontent.com/',"http://2.bp.blogspot.com",$bl4);
														$bl6=preg_replace('/https:\/\/lh5.googleusercontent.com/',"http://1.bp.blogspot.com",$bl5);
														$bl7=preg_replace('/https:\/\/lh6.googleusercontent.com/',"http://4.bp.blogspot.com",$bl6);
													
														$bl8=preg_replace('/http:\/\/lh3.googleusercontent.com/',"http://3.bp.blogspot.com",$bl7);
														$bl9=preg_replace('/http:\/\/lh4.googleusercontent.com/',"http://2.bp.blogspot.com",$bl8);
														$bl10=preg_replace('/http:\/\/lh5.googleusercontent.com/',"http://1.bp.blogspot.com",$bl9);
														$bl11=preg_replace('/http:\/\/lh6.googleusercontent.com/',"http://4.bp.blogspot.com",$bl10);
														
														$new2=preg_replace('/data.manga24h.com\/manga/',"data2.manga24h.com/manga1",$bl11);
														$new3=preg_replace('/media3.manga24h.com/',"data2.manga24h.com",$new2);
														$new4=preg_replace('/data.manga24h.com\/data/',"data2.manga24h.com/data",$new3);
														$new5 = preg_replace('/\/s[0-9]*\//', '/s0/',$new4);
														
														
														$pos = strpos($new5, "blogspot.com");
														if ($pos === false) {
														   
														} else {
									if(!preg_match("/\/s[0-9]*\//",$new5)&&!preg_match("/\/h[0-9]*\//",$new5)&&!preg_match("/\/w[0-9]*\//",$new5)&&!preg_match("/\/d\//",$new5)){
										$new5 = preg_replace('/\/[a-z0-9-_%\.]*\.(jpg|png|gif|jpeg)/i', '/s0/1.jpg',$new5);
									}
																																		
														}
																												
																												
														
														
														array_push($row['chapter_link'],$new5);
														$h++;
	

											
		
									
	
								}
								$row['chapter_page']=$h;	
							
							
							} //End of else getlink
							
							
							
							// GET LINK OKE ROI, GIO INSERT VAO CHAPTERK_CONTENT
							$db->nonquery("delete from chapterk_content where chap_id=".$id);
							$datan=array("chap_content"=>serialize($row['chapter_link']),"chap_id"=>$id,"chap_page"=>$row['chapter_page']);
							$db->insert("chapterk_content",$datan);
							$db->nonquery("update chapterk2 set op=1 where id=".$id);
	}//End of else op
	
							
							$data=$row;
			
		//SET CACHE KHI TRY CACHE ==TRUE	
		if($try_cache){
		
			$cache->save("manga_get_chapter_detail".$id,$data,3600,'chapter/'.$sub);	
		}
	}
	
	return $data;
	
	

}

function mangaf2_get_manga_theloai($str){
		
		$theloaiar=explode(",",$str);
		$str='';
		$str2='';
			$db2=new db;
			$db2->connect();
			foreach($theloaiar as $value ){
				$value=trim($value);
				$str.="<a href='".mangaf1_url_manga_theloai($value,khongdau($value))."'> <i class='fa fa-tags'></i> ".$value." </a> ";
				$str2.="<a href='".mangaf1_url_manga_theloai($value,khongdau($value))."'>".$value."</a>, ";
										
			}
			$str=substr($str,0,strlen($str)-1);
			$str2=substr($str2,0,strlen($str2)-1);
		$db2->close();
		$row['1']=$str;
		$row['2']=$str2;
		return $row;
}
function mangaf2_get_manga_nguon($str){
		
									
									if(strpos($str,',')!==false){
										$theloaiar=explode(",",$str);
									}else
									if(strpos($str,'|')!==false){
										$theloaiar=explode("|",$str);
									}else
									if(strpos($str,'+')!==false){
										$theloaiar=explode("+",$str);
									}
									else
									if(strpos($str,';')!==false){
										$theloaiar=explode(";",$str);
									}
									else{
									
										$theloaiar=explode(",",$str);
									}
			$str='';
		
			
			foreach($theloaiar as $value ){
				$value=mangaf2_manga_nguon_filter(trim($value));
				$str.="<a href='".mangaf1_url_manga_nguon(khongdau($value))."'> ".$value." </a>,";
				
										
			}
			$str=substr($str,0,strlen($str)-1);
		
		
		
		
		return $str;
}

function mangaf2_manga_nguon_filter($str){
	$str=str_ireplace('manga24h.com','Manga24h',$str);
	$str=str_ireplace('comicvn.net','Comicvn',$str);
	$str=str_ireplace('truyentranh8.com','Truyentranh8',$str);
	$str=str_ireplace('vnhsaring.net','Vnsharing',$str);
	$str=str_ireplace('blogtruyen.com','Blogtruyen',$str);
	$str=str_ireplace('vechai.info','Vechai',$str);
	$str=str_ireplace('.info','',$str);
	$str=str_ireplace('.com','',$str);
	$str=str_ireplace('.net','',$str);
	$str=str_ireplace('.vn','',$str);
	
	return $str;
}
function mangaf2_manga_get_nguon_info($str){
	$fan= explode(',',$str);
	$num=array_rand($fan);
	
	$md=new Model;
	foreach($fan as $val){
		$fan_alias=khongdau(mangaf2_manga_nguon_filter($val));
			$md->select='*';
			$md->where="alias='".$fan_alias."'";
			$md->getdata('anime_fansub');
			if($md->num_rows()>0){
					$res= $md->fetchOne();
					
					if($res['hinhanh']==''){	$res['hinhanh']='http://1.bp.blogspot.com/-rvJ_cba4Ubk/VQa5KahBWEI/AAAAAAAASKI/MP-fcGbPjqw/s0/amethyst.png';}
					if($res['trangchu']==''){	$res['trangchu']=HOME;							}
					if($res['facebook']==''){ 	$res['facebook']=HOME;							}
					if($res['dangky']==''){		$res['dangky']=HOME;							}  
					$res['url']=mangaf1_url_manga_nguon(khongdau($res['name']))  ;    
			}
			$data[]=$res;
	
	}
return $data;
	
}



function mangaf2_manga_get_tag($str){
			if($str){
					
					
							$tagar=explode(",",$str);
							$tag="";
										foreach($tagar as $value ){
											$value=trim($value);
											$tag.="<a href='".mangaf1_url_manga_tag(khongdau($value))."'>$value</a> ,";
																								
										}
										
					return substr($tag,0,strlen($tag)-1);
							
				}

}





function mangaf2_manga_get_update($number){
	$cache=new cache;
	$data2=$cache->load('manga_get_update'.$number);
	if(!$data2){
		$db=new db;
		$db->connect();
		$db->query("SELECT mg.id,mg.hinhanh,mg.theloai, mg.name,mg.gioithieu,mg.alias,chap.status,chap.ten, chap.id as chapid,chap.hot,chap.new, chap.seourl,chap.index_show,chap.ngaythem
								FROM chapterk2 AS chap, anime AS mg
								WHERE  chap.index_show!='' and chap.manga = mg.id
								ORDER BY chap.ngaythem desc,chap.id desc
								LIMIT 0 , ".$number);
				$dt=$db->fetchAll();
			foreach($dt as $row){
				$row['hinhanh']=preg_replace('/https/','http',$row['hinhanh']); 

				$row['mgurl']=mangaf1_url_manga($row['id'],$row['alias']);
				 $row['chapurl']=mangaf1_url_manga_episode($row['chapid'],$row['seourl']);
				 $data[]=$row;
			}
			
			//Foreach data phat đa
				$ngay=$data[0]['ngaythem'];
							$row2['ngay']=$ngay;
							$data2=array();
							foreach($data as $row){
									if($ngay!=$row['ngaythem']){
										
												$data2[]=$row2;  //---------Clear row 2
												$ngay=$row['ngaythem'];
												$row2=array();
												$row2['ngay']=$ngay;
												$row2['data'][]=$row;
										
									}else{
											$row2['data'][]=$row;
									}
								
							}
								$data2[]=$row2;
			
			//End foreach data
	$cache->save("manga_get_update".$number,$data2,300);							
	}
	
	
	return $data2;
}

function mangaf2_get_manga_list($dt){
			foreach($dt as $row){
				
				$row['mgurl']=mangaf1_url_manga($row['id'],$row['alias']);
				 $row['chapurl']=mangaf1_url_manga_episode($row['chapid'],$row['seourl']);
				 $data[]=$row;
			}
			
			//Foreach data phat đa
				$ngay=$data[0]['ngaythem'];
							$row2['ngay']=$ngay;
							$data2=array();
							foreach($data as $row){
									if($ngay!=$row['ngaythem']){
										
												$data2[]=$row2;  //---------Clear row 2
												$ngay=$row['ngaythem'];
												$row2=array();
												$row2['ngay']=$ngay;
												$row2['data'][]=$row;
										
									}else{
											$row2['data'][]=$row;
									}
								
							}
									$data2[]=$row2;
								return $data2;
								
}


// ---------------------------- VIEW MANGA

function mangaf2_get_pick(){
	$cache=new cache;
	$str=$cache->load('get_anime_pick','anime');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query("select id,name,alias,view_day,ep_moinhat,ep_moinhaturl,hinhanh,tinhtrang,nguon,theloai from anime where pick=1 order by view_day desc limit 0,10");
		$str=$db->fetchAll();
		$cache->save('get_anime_pick',$str,1800,'anime');
	} // End !$data
	
	return $str;

} // End of function get truyen view day


function mangaf2_get_new(){
	$cache=new cache;
	$str=$cache->load('get_anime_new','anime');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query("select id,name,alias,view_day,ep_moinhat,ep_moinhaturl,hinhanh,tinhtrang,nguon,theloai from anime where duyet=1 order by id desc limit 0,15");
		$str=$db->fetchAll();
		$cache->save('get_anime_new',$str,1800,'anime');
	} // End !$data
	
	return $str;

} // End of function get truyen view day



function mangaf2_get_view(){
	
	$cache=new cache;
	$str=$cache->load('get_anime_view2','anime');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query('select id,name,alias,views,hinhanh,ep_moinhat,ep_moinhaturl,tinhtrang from anime order by views desc limit 0,10');
		
		$str= "<div class='list-group list-view'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
				$j=1;
			foreach($dt as $row){
						 $str.=" <li class='list-group-item'><a href='".mangaf1_url_manga($row['id'],$row['alias'])."' class='manga'>".$row['name']."</a>
						 		<span class='featured-item-new-chapt'><a href='".$row['ep_moinhaturl']."'>Chap 696</a></span>
							  <div class='clearfix'></div>
							  </li>"; 
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_anime_view',$str,1800,'anime');
	} // End !$data
	
	return $str;

}// End function get truyen view
function mangaf2_get_view_day(){
	$cache=new cache;
	$str=$cache->load('get_anime_view_day','anime');
	
	if(!$str){
		
		$db=new db;
		$db->connect();
		$db->query("select id,name,alias,view_day,ep_moinhat,ep_moinhaturl,hinhanh,tinhtrang from anime where last_update_view='".date('Y-m-d')."' order by view_day desc limit 0,10");
		
	$str= "<div class='list-group list-view'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
				 			 $str.=" <li class='list-group-item'><a href='".mangaf1_url_manga($row['id'],$row['alias'])."' class='manga'>".$row['name']."</a>
						 		<span class='featured-item-new-chapt pull-right'><a href='".str_replace('http://manga24h.com','http://truyencuatui.info/truyentranh',$row['ep_moinhaturl'])."'>".$row['ep_moinhat']."</a></span>
							  <div class='clearfix'></div>
							  </li>"; 
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_anime_view_day',$str,1800,'anime');
	} // End !$data
	
	return $str;

} // End of function get truyen view day

function mangaf2_get_view_week(){
	$cache=new cache;
	$str=$cache->load('mangaf2_get_view_week','anime');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query("select id,name,alias,view_week,hinhanh,ep_moinhat,ep_moinhaturl,tinhtrang from anime where WEEK(last_update_view, 3) = '" . date('W') . "' order by view_week desc limit 0,10");
		
	$str= "<div class='list-group list-view'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
						 $str.=" <li class='list-group-item'><a href='".mangaf1_url_manga($row['id'],$row['alias'])."' class='manga'>".$row['name']."</a>
						 		<span class='featured-item-new-chapt pull-right'><a href='".str_replace('http://manga24h.com','http://truyencuatui.info/truyentranh',$row['ep_moinhaturl'])."'>".$row['ep_moinhat']."</a></span>
							  <div class='clearfix'></div>
							  </li>"; 
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('mangaf2_get_view_week',$str,1800,'anime');
	} // End !$data
	
	return $str;

} // End of function get truyen view week


function mangaf2_get_view_month(){
	$cache=new cache;
	$str=$cache->load('mangaf2_get_view_month','anime');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query("select id,name,alias,view_month,hinhanh,ep_moinhat,ep_moinhaturl,tinhtrang from anime where MONTH(last_update_view) = '" . date('m') . "' order by view_month desc limit 0,10");
	$str= "<div class='list-group list-view'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
							 $str.=" <li class='list-group-item'><a href='".mangaf1_url_manga($row['id'],$row['alias'])."' class='manga'>".$row['name']."</a>
						 		<span class='featured-item-new-chapt pull-right'><a href='".str_replace('http://manga24h.com','http://truyencuatui.info/truyentranh',$row['ep_moinhaturl'])."'>".$row['ep_moinhat']."</a></span>
							  <div class='clearfix'></div>
							  </li>"; 
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('mangaf2_get_view_month',$str,1800,'anime');
	} // End !$data
	
	return $str;

} // End of function get truyen view month



function mangaf2_get_comment(){
	$cache=new cache;
	$str=$cache->load('list_truyen_comment','truyen');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select * from comment  order by id desc limit 0,12");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			
			
			
            $str='';
              $j=1;
               foreach($data as $row){
                 
					 
					 				if($j%2==0){
									$str.='<div class="item-even each-comment-item">';
									}else{
									$str.='<div class="item-odd each-comment-item">';
									}
										$str.='<a title="'.$row["tieude"].'" href="'.str_replace('http://manga24h.com','http://truyencuatui.info/truyentranh',$row['url']).'" class="each-comment-title">'.cutstring($row["tieude"], 28).'</a>
										<span class="comment-time">'.time_ago($row['time_add']).'</span>
										<div class="clr"></div>
										<p class="each-comment-des">'.cutstring($row['noidung'], 100).'</p>
									</div>';
					 
					  $j++;
					  
					  
					  
					  
					  
                   } // End foreach

			
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_truyen_comment',$str,600,'truyen');
	}
	
	return $str;

} 

function mangaf2_generate_tags($type,$data){
	switch($type){
		case "manga":
			$val['title']=$data." | Đọc truyện online - " . DOMAIN_NAME;
			$val['mota']="Đọc truyện tranh $data chap mới nhất, trọn bộ trên " . DOMAIN_NAME;
			$val['keyword']="Doc truyen $data chap moi nhat,doc truyen $data tron bo,$data full,$data tron bo,$data chap moi nhat";
			break;
		case "chap":
			$data['chap']=str_ireplace('chapter','chap',$data['chap']);
			$val['title']=$data['manga']." ".str_ireplace('chapter','chap',$data['chap'])." | Đọc truyện online - " . DOMAIN_NAME;
			$val['mota']="Đọc truyện ".$data['manga']." ".$data['chap']." tiếng việt nhanh nhất trên " . DOMAIN_NAME;
			$val['keyword']=$data['manga']." ".$data['chap']." tiếng việt,".$data['manga']." ".$data['chap'].",doc truyen ".$data['manga']." ".$data['chap']." tieng viet,".$data['manga']." chuong ".$data['chap'];
			break;
	}
	
	
	return $val;



}
